package com.xcw.annotation;

import com.xcw.entity.req.UserLoginParams;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.Arrays;


/**
 * @Author bhx
 * @Date 2022/6/23 22:13
 * @Description 用户鉴权方案
 */
@Aspect
@Component
@Slf4j
public class MethodLogAspect {


    @Pointcut("@annotation(com.xcw.annotation.IntoLog)")
    public void get(){

    }

    @SneakyThrows
    @Around("@annotation(com.xcw.annotation.IntoLog)")
    public Object processPerform(ProceedingJoinPoint point){

        IntoLog intoLog = AnnotationUtils.getAnnotation(((MethodSignature) point.getSignature()).getMethod(), IntoLog.class);
        String methodLog = intoLog.logInfo();
        String auth = intoLog.auth();
        Object[] args = point.getArgs();

        Arrays.stream(args).forEach(System.out::println);

        UserLoginParams userLoginParams = (UserLoginParams)args[0];
        System.out.println(userLoginParams);
        Boolean checkAuth = authCheck(userLoginParams);
        Assert.isTrue(checkAuth, "用户没有权限！！");
        log.info("{}" + userLoginParams);
        return point.proceed();
    }

    private Boolean authCheck(UserLoginParams paramValue) {
        System.out.println(paramValue);
        // 操作数据库查询数据
        return true;
    }
}
